package extend.js
{
	import com.gengine.core.frame.*;
	import com.gengine.global.*;
	
	import extend.php.*;
	
	import flash.events.*;
	import flash.external.*;
	import flash.system.*;
	
	public class JSASSender extends Object
	{
		private var type:int = 0;
		
		public function JSASSender()
		{
			if (_instance)
			{
				throw new Error("单例！");
			}
			
			Security.allowDomain("*");
		}
		
		public function start() : void
		{
			Global.stage.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
		
		private function onEnterFrameHandler(event:Event) : void
		{
			if (!ExternalInterface.available) 
			
			Global.stage.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
			init();
			callJsSetInterval();
		}
		
		private function init() : void
		{
			if (ExternalInterface.available)
			{
				ExternalInterface.marshallExceptions = true;
				
				try
				{
					ExternalInterface.addCallback("jsCall", jsCall);
					ExternalInterface.addCallback("jsClose", jsClose);
				}
				catch (e:*){
					
				}
			}
		}
		
		private function callJsSetInterval() : void
		{
			if (!ExternalInterface.available) 
			
			ExternalInterface.call("asCallJs");
		}
		
		public function callMapResize(value:int) : void
		{
			if (!ExternalInterface.available)
			{
				
			}
			if (this.type == 1)
			{
				this.type = 0;
			}
			else
			{
				this.type = 1;
			}
			ExternalInterface.call("callMapResize", this.type);
		}
		
		public function jsCall() : void
		{
			FrameManager.flashFrame.dispatchEvent();
		}
		
		public function jsClose() : void
		{
			if (PHPSender.isCreateRole)
			{
				PHPSender.sendToURLByPHP(SenderType.FlashClose);
			}
		}
		
		private static var _instance:JSASSender;
		public static function get instance() : JSASSender
		{
			if (!_instance)
			{
				_instance = new JSASSender;
			}
			return _instance;
		}
	}
}